package com.demo2do.strawberry.web.controller.advice;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.demo2do.core.BusinessException;

/**
 * Global Exception Handler shared by all controllers
 * 
 * @author Downpour
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	
	/**
	 * Handle BusinessException
	 * 
	 * @param businessException
	 * @param request
	 * @return
	 */
	@ExceptionHandler(BusinessException.class)
	public String handleBusinessException(BusinessException businessException, HttpServletRequest request) {
		
		// set businessException messages
		if(businessException.getMessage() != null) {
			request.setAttribute("errorMessage", businessException.getMessage());
		}

		// TODO how to handle if targetURL is null?
		
		return businessException.getTargetURL();
	}
}
